package com.psl.services.frame;

import com.psl.Dao.Conn;
import com.psl.model.helpsetModel;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mioko on 2017/9/17.
 */
public class helpsetServices {
    public List queryone(String groupId) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List help = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT groupId,name,acticle FROM helpset where groupId=?");
            pstmt.setString(1,groupId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                helpsetModel hm=new helpsetModel();
                hm.setGroupId(rs.getInt(1));
                hm.setName(rs.getString(2));
                hm.setActicle(rs.getString(3));
                help.add(hm);
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("help", help);
            }
            return help;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public boolean savehelp(String groupId, String name,String acticle) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("UPDATE helpset set name=?,acticle=? WHERE groupId=?");
            pstmt.setString(1, name);
            pstmt.setString(2, acticle);
            pstmt.setString(3, groupId);
            pstmt.execute();
            HttpServletRequest request=ServletActionContext.getRequest();
            request.setAttribute("success","保存成功");
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

}